package com.ricky;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;

import java.io.File;

/**
 * Created by baiguantao on 2017/12/19.
 */
public class OpenOne extends AnAction {

    /**
     * 进行class文件目录定位
     * @param e
     */
    @Override
    public void actionPerformed(AnActionEvent e) {
        try {
            Project project=e.getData(PlatformDataKeys.PROJECT);
//            String curpath=e.getData(PlatformDataKeys.PROJECT_FILE_DIRECTORY).getPath();//项目目录
            String curpath=e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY)[0].getPath();//当前文件路径
            String realPath=getPath(curpath,project);
            String path=project.getBasePath();//基础路径
            FileUtil.openDir(curpath,project);
        } catch (Exception f) {
            f.printStackTrace();
        }
    }
    private  String getPath(String curpath,Project project){
        String tempath=curpath.substring(0,curpath.lastIndexOf("/"));;
        curpath=curpath.replaceAll("/main","").replaceAll("/resources","").replaceAll("/java","");
        System.out.println(curpath);
        String realPath="";
        String targetpath=curpath.replaceAll("src","target/classes").replaceAll("\\.java",".class");
        System.out.println("targetpath:"+targetpath+"");
        File targetpathFile=new File(targetpath);
        //maven项目
        //d:/projecyname/src/com/demo.java->d:/projecyname/target/classes/com/demo.class
        if(targetpathFile.exists()){
            realPath=targetpath;
        }
        String outpath=curpath.replaceAll("src","out/classes").replaceAll("\\.java",".class");
        System.out.println("outpath:"+outpath);
        File outpathFile=new File(outpath);
        //d:/projecyname/src/com/demo.java->d:/projecyname/out/classes/com/demo.class
        if(outpathFile.exists()){
            realPath=outpath;
        }
        String outpath2=curpath.replaceAll("src","out/production/"+project.getName()).replaceAll(".java",".class");
        System.out.println("outpath2:"+outpath2);
        File outpath2File=new File(outpath2);
        //d:/projecyname/src/com/demo.java->d:/projecyname/out/production/projecyname/com/demo.class
        if(outpath2File.exists()){
            realPath=outpath2;
        }

        String classpath=curpath.replaceAll("src","classes/production/"+project.getName()).replaceAll(".java",".class");
        System.out.println("classpath:"+classpath);
        File classpathFile=new File(classpath);
        //d:/projecyname/src/com/demo.java->d:/projecyname/classes/production/projecyname/com/demo.class
        if(classpathFile.exists()){
            realPath=classpath;
        }

        if (realPath=="") {
            /*Messages.showWarningDialog(project,"源文件文件:"+curpath+"\n"+"target："+targetpath
                    +"\n"+"outpath:"+outpath
                    +"\n"+"classpath:"+classpath
                    +"\n"+"production-outpath2"+outpath2+"\n"+"不存在","搜索结果");*/
            Messages.showWarningDialog(project,"暂不支持您的编译路径,请参阅插件说明进行配置","搜索结果");
        }
        return  realPath;
    }
    private void test(AnActionEvent e){
        Editor editor=e.getData(PlatformDataKeys.EDITOR);

        SelectionModel selectionModel = editor.getSelectionModel();
        selectionModel.getSelectedText();//选中文本内容

    }

    public static void main(String[] args) {
        String curpath="C:/Users/temp/IdeaProjects/rqb_payment/src/main/java/com/rqbao/payment/service/trade/TradeManager.java";
        curpath=curpath.replaceAll("/main","").replaceAll("/resources","").replaceAll("/java","");
        String targetpath=curpath.replaceAll("src","target/classes").replaceAll("\\.java",".class");
        System.out.println("targetpath:"+targetpath+"-----");
        String outpath=curpath.replaceAll("src","out/classes").replaceAll("\\.java",".class");
        String outpath2=curpath.replaceAll("src","out/production/"+"payment/").replaceAll(".java",".class");
        System.out.println("outpath:"+outpath+"-----");
        System.out.println("outpath2:"+outpath2+"-----");
        File targetpathFile=new File(targetpath);
        File outpathFile=new File(outpath);
        File outpath2File=new File(outpath2);
        String realPath="";
        if(targetpathFile.exists()){
            realPath=targetpath;
        }
        if(outpathFile.exists()){
            realPath=outpath;
        }
        if(outpath2File.exists()){
            realPath=outpath2;
        }
        realPath=realPath;
        System.out.println(realPath);
    }

    /*
    *
    *
    * C:/Users/temp/IdeaProjects/rqb_payment/src/main/java/com/rqbao/payment/service/trade/TradeManager.java--------
    * C:/Users/temp/IdeaProjects/rqb_payment/target/classes/main.class
    * /com/rqbao/payment/service/trade/TradeManager.class--------
    * C:/Users/temp/IdeaProjects/rqb_payment/out/classes/main.class/com/rqbao/payment/service/trade/TradeManager.class--------C:/Users/temp/IdeaProjects/rqb_payment/out/production/rqb_payment/main.class/com/rqbao/payment/service/trade/TradeManager.class--------不存在
    * */
}
